package com.zhiche.lisa.bms.service.view.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.view.FeeBillMapper;
import com.zhiche.lisa.bms.dao.model.view.FeeBill;
import com.zhiche.lisa.bms.dao.model.view.FeeBillDetail;
import com.zhiche.lisa.bms.service.view.FeeBillDetailService;
import com.zhiche.lisa.bms.service.view.FeeBillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author linbao
 * @since 2019-03-04
 */
@Service
@Slf4j
public class FeeBillServiceImpl extends ServiceImpl<FeeBillMapper, FeeBill> implements FeeBillService {

    @Autowired
    private FeeBillDetailService feeBillDetailService;

    /**
     * 获取其他扣款
     *
     * @param waybillIdList
     * @return
     */
    @Override
    public Map<String, BigDecimal> getFeeBillDuceSumByDtlWaybillId(List<String> waybillIdList) {
        if (CollectionUtils.isNotEmpty(waybillIdList)) {
            waybillIdList.forEach(waybillId -> {

            });
        }

        return null;
    }

    /**
     * 根据OR号获取其他扣款
     *
     * @param waybillDtlId
     * @return
     */
    @Override
    public BigDecimal getFeeBillDuceSumByDtlWaybillId(String waybillDtlId) {
        BigDecimal otherSum = BigDecimal.ZERO;
        if (StringUtils.isNotBlank(waybillDtlId)) {
            EntityWrapper<FeeBillDetail> dtlEw = new EntityWrapper<>();
            dtlEw.eq("waybill_dtl_id", waybillDtlId);
            dtlEw.setSqlSelect("bill_code");
            List<Object> objectList = feeBillDetailService.selectObjs(dtlEw);
            if (CollectionUtils.isNotEmpty(objectList)) {
                for (Object obj : objectList) {
                    EntityWrapper<FeeBill> billEw = new EntityWrapper<>();
                    billEw.eq("bill_code", obj);
                    List<FeeBill> feeBillList = this.selectList(billEw);
                    if (CollectionUtils.isNotEmpty(feeBillList)) {
                        EntityWrapper<FeeBillDetail> billDtlCountEw = new EntityWrapper<>();
                        billDtlCountEw.eq("bill_code", obj);
                        int count = feeBillDetailService.selectCount(billDtlCountEw);
                        FeeBill feeBill = feeBillList.get(0);
                        if (count != 0) {
                            if (feeBill != null && feeBill.getDeductSum() != null) {
                                otherSum = otherSum.add(feeBill.getDeductSum().divide(new BigDecimal(count), 3, BigDecimal.ROUND_HALF_UP));
                            }
                        } else {
                            otherSum = otherSum.add(feeBill.getDeductSum());
                        }
                    }
                }
            }
        }
        return otherSum;
    }
}
